//
//  WBBaseViewController.swift
//  weibo
//
//  Created by z先生 on 17/1/5.
//  Copyright © 2017年 SZT. All rights reserved.
//

import UIKit

class WBBaseViewController: UIViewController {

    //用户登录标记
    var userLogon = false
    
    var visitorInfo:[String:String]?
    
    var tableView : UITableView?
    var refreshControl :UIRefreshControl?
    
    var isPullup = false
    
    lazy var navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    
    lazy var navItem = UINavigationItem()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        loadData()
        
    }

    override var title: String?{
        
        didSet{
            
           navItem.title = title
            
        }
    }
    
    func loadData(){
        
        func loadData(){
            
            refreshControl?.endRefreshing()
            
        }
    }
}
//swift中利用extension 可以把函数按功能分类管理

//extension中不能有属性  不能重写父类方法
extension WBBaseViewController{
    
    func setupUI() {
        
//        view.backgroundColor = UIColor.cz_random()
        
        
        automaticallyAdjustsScrollViewInsets = false
        
        setupNavigationBar()
        
        userLogon ? setupTableView() : setupVistorView()
        
        
    }
    
    func setupNavigationBar(){
        
        view.addSubview(navigationBar)
        
        navigationBar.items = [navItem]
        
        
        navigationBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.darkGray]

        navigationBar.tintColor = UIColor.orange
    }
    
    func setupTableView(){
        
        tableView = UITableView(frame: view.bounds, style: .plain)
        
        view.insertSubview(tableView!, belowSubview: navigationBar)
        
        tableView?.dataSource = self
        
        tableView?.delegate = self
        
        tableView?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
        
        
        refreshControl = UIRefreshControl()
        
        tableView?.addSubview(refreshControl!)
        
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }
    
    func setupVistorView(){
        
        let vistorView = WBVistorView(frame: view.bounds)
        
        view.insertSubview(vistorView, belowSubview: navigationBar)
        
        vistorView.visitorInfo = visitorInfo
        
        vistorView.loginButton.addTarget(self, action: #selector(login), for: .touchUpInside)
        
        vistorView.registerButton.addTarget(self, action: #selector(register), for: .touchUpInside)
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(register))
        
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(login))
        
        
    }
}

extension WBBaseViewController:UITableViewDataSource,UITableViewDelegate{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        return UITableViewCell()
        
    }
    
    //显示最后一行的时候做上拉刷新
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let row = indexPath.row
        
        let section = tableView.numberOfSections - 1
        
        print("section --\(section)")
        
        if row < 0 || section < 0 {
            
            return
        }
        
        let count = tableView.numberOfRows(inSection: section)
        
        if row == (count - 1) && !isPullup {
            
            print("上拉刷新")
            
            isPullup = true
            
            loadData()
        }
        
        
    }
}
extension WBBaseViewController{
    
    @objc func login(){
        
        print("用户登录")
    }
    
    @objc func register(){
        
        print("用户注册")
        
    }
}
